package com.project.service.MQ;


import com.project.bean.OrderBean;
import com.project.bean.OrderInfoBean;
import com.project.dto.riderToUser.InfoDto;
import com.project.dto.riderToUser.OrderDto;
import com.project.dto.userToMerchant.OrderInfoDto;
import com.project.service.IOrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 卢泓佚  414814103@qq.com on 2021/9/7
 */
@Service
public class MQListenerService {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = "RiderQueue")
    public void RiderQueue(OrderDto orderDto, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws Exception {
        System.out.println("进入骑手开始送订单队列：" + orderDto);
        if(orderDto.getStatue().equals("可取货")){
            orderService.addOrderRiderAndUpdateStatus(orderDto.getOrderCode(),orderDto.getRiderCode(),
                    orderDto.getRiderName(),2);
        }
        //checkOrder(orderDto, channel, tag);
    }

    @RabbitListener(queues = "RiderFinishOrderQueue")
    public void RiderFinishOrderQueue(InfoDto infoDto) throws Exception {
        System.out.println("进入骑手完成送订单队列：" + infoDto);
        OrderBean orderBean =  orderService.finishOrderAndUpdateStatus(infoDto.getOrderCode(),3,infoDto.getEndtime().substring(0,infoDto.getEndtime().lastIndexOf(".")));
        List<OrderInfoDto> infoDtoList = new ArrayList<>();
        List<OrderInfoBean> infoBeanList = orderBean.getInfoList();
        for (int i = 0; i < infoBeanList.size(); i++) {
            OrderInfoDto dto = new OrderInfoDto();
            BeanUtils.copyProperties(infoBeanList.get(i), dto);
            infoDtoList.add(dto);
        }
        com.project.dto.userToMerchant.OrderDto orderDto = new com.project.dto.userToMerchant.OrderDto();
        BeanUtils.copyProperties(orderBean, orderDto);
        orderDto.setInfoList(infoDtoList);
        CorrelationData correlationData = new CorrelationData(infoDto.getOrderCode());
        rabbitTemplate.convertAndSend("userDirectExchange", "User.toBackQueue", orderDto , correlationData);
        //channel.basicAck(tag,false);//是否批量确认
    }


    private void checkOrder(OrderDto orderDto, Channel channel, long tag) throws InterruptedException, IOException {
        if (null != orderDto) {
            //手动确认
            //channel.basicAck(tag,false);//是否批量确认
            //rabbitTemplate.convertAndSend("orderDirectExchange","order.B",orderDto);
        }
    }
}
